package com.lb.jwitter.action.report;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.CommonService;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXADMChiNhanh;
import com.lb.jwitter.service.TblQLDTTXADMChiNhanhService;
import com.lb.jwitter.service.TblQLDTTXADMKhoa;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHoc;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHocService;
import com.lb.jwitter.service.TblQLDTTXADMKhoaService;
import com.lb.jwitter.service.TblQLDTTXADMNganh;
import com.lb.jwitter.service.TblQLDTTXADMNganhService;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhi;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiBienLaiService;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiService;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;
import com.lb.jwitter.service.TblQLDTTXAHocVienTaiKhoan;
import com.lb.jwitter.service.TblQLDTTXAHocVienTaiKhoanService;
import com.lb.jwitter.service.TblQLDTTXALop;
import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
import com.lb.jwitter.service.TblQLDTTXALopService;
import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQLDTTXAQuiDinhHocPhi;
import com.lb.jwitter.service.TblQLDTTXAQuiDinhHocPhiService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienHocPhiHocKyReportAction{
    private Message msg; 
    public Message getMsg() { return msg; }

    private Long lopId;
    private Long khoaId;
    private Long nganhId;
    private Long khoaHocId;
    private Long chiNhanhId;
    private Long hocKyId;
    
    private String report;
    
    private TblQLDTTXAHocVienHocPhiService tblQLDTTXAHocVienHocPhiService;
    private TblQLDTTXAHocVienHocPhiMonHocService tblQLDTTXAHocVienHocPhiMonHocService;
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;
    private TblQLDTTXALopService tblQLDTTXALopService;
    private TblQLDTTXADMKhoaService tblQLDTTXADMKhoaService;
    private TblQLDTTXADMKhoaHocService tblQLDTTXADMKhoaHocService;
    private TblQLDTTXADMNganhService tblQLDTTXADMNganhService;
    private TblQLDTTXADMChiNhanhService tblQLDTTXADMChiNhanhService;
    private TblQLDTTXAQuiDinhHocPhiService tblQLDTTXAQuiDinhHocPhiService;
    
    private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService;
    private TblQLDTTXAHocVienTaiKhoanService tblQLDTTXAHocVienTaiKhoanService;
    private TblQLDTTXAHocVienHocPhiBienLaiService tblQLDTTXAHocVienHocPhiBienLaiService;
    
    private List<TblQLDTTXADMKhoa> tblQLDTTXADMKhoas;
    private List<TblQLDTTXALop> tblQLDTTXALops;
    private List<TblQLDTTXADMNganh> tblQLDTTXADMNganhs;
    private List<TblQLDTTXADMKhoaHoc> tblQLDTTXADMKhoaHocs;
    private List<TblQLDTTXADMChiNhanh> tblQLDTTXADMChiNhanhs;
    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams;
    private   List<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs;
    private   List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs;

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService;

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_hocPhi", "xem")){ 
        return "noprivilege"; 
    } 
 
        tblQLDTTXADMChiNhanhService = (TblQLDTTXADMChiNhanhService) context.getBean("tbl_QLDTTXA_DM_ChiNhanhClient");
        tblQLDTTXADMKhoaHocService = (TblQLDTTXADMKhoaHocService) context.getBean("tbl_QLDTTXA_DM_KhoaHocClient");
        tblQLDTTXADMKhoaService = (TblQLDTTXADMKhoaService) context.getBean("tbl_QLDTTXA_DM_KhoaClient");
        tblQLDTTXADMNganhService = (TblQLDTTXADMNganhService) context.getBean("tbl_QLDTTXA_DM_NganhClient");
        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
        tblQLDTTXALopService = (TblQLDTTXALopService) context.getBean("tbl_QLDTTXA_LopClient");
        
//        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
//        tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient"); 


        msg = new Message(); 
        
        tblQLDTTXADMChiNhanhs = tblQLDTTXADMChiNhanhService.findAlltblQLDTTXADMChiNhanh(0, 0);
        tblQLDTTXADMKhoaHocs = tblQLDTTXADMKhoaHocService.findAlltblQLDTTXADMKhoaHoc(20, 1);
        tblQLDTTXADMKhoas = tblQLDTTXADMKhoaService.findAlltblQLDTTXADMKhoa(0, 0);
        tblQLDTTXADMNganhs = tblQLDTTXADMNganhService.findAlltblQLDTTXADMNganh(0, 0);
        tblQLDTTXALops = tblQLDTTXALopService.findAlltblQLDTTXALop(0,0 );
        
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);	
        } 
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 20, 1);
        
        
        return Action.SUCCESS;
	 }catch(Exception e){ 
	     System.out.println("Loi Report tblQLDTTXABienNhanHoSo Action"); 
	     e.printStackTrace(); 
	     return Action.ERROR; 
	}
}
 
 
 
 
 public String execute() throws Exception 
 { 
  try{ 	
      Map session = ActionContext.getContext().getSession();
      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
      if( taiKhoanDangNhap == null) return "noprivilege";
     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_BienNhanHoSo", "xem")){ 
         return "noprivilege"; 
     } 
  
//         tblQLDTTXADMChiNhanhService = (TblQLDTTXADMChiNhanhService) context.getBean("tbl_QLDTTXA_DM_ChiNhanhClient");
//         tblQLDTTXADMKhoaHocService = (TblQLDTTXADMKhoaHocService) context.getBean("tbl_QLDTTXA_DM_KhoaHocClient");
//         tblQLDTTXADMKhoaService = (TblQLDTTXADMKhoaService) context.getBean("tbl_QLDTTXA_DMKhoaClient");
//         tblQLDTTXADMNganhService = (TblQLDTTXADMNganhService) context.getBean("tbl_QLDTTXA_DM_NganhClient");
//         tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
         tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
         tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient"); 
         tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService) context.getBean("tbl_QLDTTXA_LopMonHocClient"); 
         tblQLDTTXAQuiDinhHocPhiService = (TblQLDTTXAQuiDinhHocPhiService) context.getBean("tbl_QLDTTXA_QuiDinhHocPhiClient"); 
         tblQLDTTXAHocVienTaiKhoanService = (TblQLDTTXAHocVienTaiKhoanService) context.getBean("tbl_QLDTTXA_HocVien_TaiKhoanClient"); 
         tblQLDTTXAHocVienHocPhiBienLaiService = (TblQLDTTXAHocVienHocPhiBienLaiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhi_BienLaiClient");
         tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient");
         
         msg = new Message(); 
         List<ConditionReport> cons = new ArrayList<ConditionReport>();
         
         /*
          * Lay ra cac mon hoc duoc mo trong hoc ky
          */
         cons.clear();
         if(hocKyId != null && !hocKyId.equals(new Long(0))){ 
         	
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocKyId.id");
             con.setValue1(hocKyId);
             con.setOperator("eq");
             cons.add(con);	
         }
         tblQLDTTXALopMonHocs = tblQLDTTXALopMonHocService.findtblQLDTTXALopMonHocByCondition(cons, 0, 0);
         
         tblQLDTTXAMonHocs = new ArrayList<TblQLDTTXAMonHoc>();
         List<Long> monHocIds = new ArrayList<Long>();
         for(TblQLDTTXALopMonHoc tblLop : tblQLDTTXALopMonHocs){
      	   if(!monHocIds.contains(tblLop.getMonHocId().getId())){
     		   tblQLDTTXAMonHocs.add(tblLop.getMonHocId());
     		   monHocIds.add(tblLop.getMonHocId().getId());
     	   }
         	
         }
         
         //lay cac thong tin hoc phi cua hoc vien
         cons.clear();
         if(hocKyId != null && !hocKyId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocKyId.id");
             con.setValue1(hocKyId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(lopId != null && !lopId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.lopId.id");
             con.setValue1(lopId);
             con.setOperator("eq");
             cons.add(con);	
         }
         if(khoaId != null && !khoaId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.khoaId.id");
             con.setValue1(khoaId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(khoaHocId != null && !khoaHocId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.khoaHocId.id");
             con.setValue1(khoaHocId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(nganhId != null && !nganhId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.nganhId.id");
             con.setValue1(nganhId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(chiNhanhId != null && !chiNhanhId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.chiNhanhId.id");
             con.setValue1(chiNhanhId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.ten");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.hoTen");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 

         List<TblQLDTTXAHocVienHocPhi> tblQLDTTXAHocVienHocPhis = tblQLDTTXAHocVienHocPhiService.findtblQLDTTXAHocVienHocPhiByCondition(cons, 0, 0);
	      
       //lay danh sach hoc vien
         cons.clear();
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("trangThaiId.id");
             con.setValue1(new Long(1));
             con.setOperator("eq");
             cons.add(con);	
         }
         if(lopId != null && !lopId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("lopId.id");
             con.setValue1(lopId);
             con.setOperator("eq");
             cons.add(con);	
         }
         if(khoaId != null && !khoaId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("khoaId.id");
             con.setValue1(khoaId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(khoaHocId != null && !khoaHocId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("khoaHocId.id");
             con.setValue1(khoaHocId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(nganhId != null && !nganhId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("nganhId.id");
             con.setValue1(nganhId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(chiNhanhId != null && !chiNhanhId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("chiNhanhId.id");
             con.setValue1(chiNhanhId);
             con.setOperator("eq");
             cons.add(con);	
         } 

//         { 
//             ConditionReport con = new ConditionReport(); 
//             con.setCol("hocVienId.trangThaiId.id");
//             con.setValue1(new Long(1));
//             con.setOperator("eq");
//             cons.add(con);	
//         }
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("ten");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hoTen");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 

         List<TblQLDTTXAHocVien> tblQLDTTXAHocViens = tblQLDTTXAHocVienService.findtblQLDTTXAHocVienByCondition(cons, 0, 0);
	      
         
 		
	     Double tongHPPhaiThu = 0.0;
         HashMap<Long, Double> tongHocPhiMonHoc = new HashMap<Long, Double>();
         
         TblQLDTTXAQuiDinhHocPhi tblQLDTTXAQuiDinhHocPhi = tblQLDTTXAQuiDinhHocPhiService.gettblQLDTTXAQuiDinhHocPhi(new Long(1));
		 
         
         List<Double> hocPhiXs = new ArrayList<Double>();
	      List<Double> hocPhiKMs = new ArrayList<Double>();
	      DecimalFormat myFormatter = new DecimalFormat("###,###.##");
	         
	      for(TblQLDTTXAMonHoc monHoc : tblQLDTTXAMonHocs){
	    	  tongHocPhiMonHoc.put(monHoc.getId(), 0.0);
	    	  Double hpmh = monHoc.getSoTinChi()*tblQLDTTXAQuiDinhHocPhi.getTienHocPhi() + tblQLDTTXAQuiDinhHocPhi.getTienHaTang();
	    	  hocPhiXs.add(hpmh);
	    	  hocPhiKMs.add(hpmh - tblQLDTTXAQuiDinhHocPhi.getTienTraLai());		    	  
	    	  
	      }
	      
		  //sinh report
		  int colNo = 0;
		  report = "<tr class='table-header'> <th style='text-align:center'> X </th> ";
		  for(int i1 = 0; i1 < hocPhiXs.size(); i1++){
			  report +="<th style='text-align:center'>" + myFormatter.format(hocPhiXs.get(i1)) + "</th>";
		  }
		  report +="</tr>";
		  
		  report += "<tr class='table-header'> <th style='text-align:center'> KM </th> ";
		  for(int i2 = 0; i2 < hocPhiKMs.size(); i2++){
			  report +="<th style='text-align:center'>" + myFormatter.format(hocPhiKMs.get(i2)) + "</th>";
		  }
		  report +="</tr></thead>";
		  
		  report +="<tbody>";
		  int stt = 0;
		  
		  // duyet tat ca hoc vien dang hoc
		  for(TblQLDTTXAHocVien hocVien : tblQLDTTXAHocViens){	
			  TblQLDTTXAHocVienHocPhi hocVienHocPhi = null;
			  for(TblQLDTTXAHocVienHocPhi hvhp : tblQLDTTXAHocVienHocPhis){
				  if(hvhp.getHocVienId().getId().equals(hocVien.getId())){
					  hocVienHocPhi = hvhp;
					  break;
				  }
			  }
			  // neu khong dang ky hoc trong hoc ky
			  if(hocVienHocPhi == null){
				  Double tienHaTang = tblQLDTTXAQuiDinhHocPhi.getTienHaTang();
				  stt++;
			      report += "<tr class='tableitem' > "; 
			      //stt
			      report += "<TD>" +stt+ "</TD>"; 
			      //mshv
			      report += "<td>" + (hocVien.getMSHV() == null ? "" : hocVien.getMSHV())+ "</td> ";
			      //lop
			      report += "<td>" +hocVien.getLopId().getTen()+ "</td> ";
			      //ho ten
			      report += "<td>" +hocVien.getHoTen()+ "</td> ";
			      report += "<td>" +hocVien.getTen()+ "</td> ";
			      //ngay sinh
			      try{
				      report += "<td>" 
				    	  +hocVien.getHoSoId().getNgaySinh().getDay()+"/"
				    	  +hocVien.getHoSoId().getNgaySinh().getMonth()+"/"
				    	  +hocVien.getHoSoId().getNgaySinh().getYear()+ "</td>";
				      
			      }catch(Exception e){
			    	  report += "<td></td>"; 
			            
			      }
			      for(TblQLDTTXAMonHoc monHoc : tblQLDTTXAMonHocs){
			    	  report += "<td></td>"; 
			      }
			      report += "<TD>0</TD>";
				  report += "<TD>0</TD>";
				  
				  // lay thong tin no hoc phi cac hoc ky truoc
				  Double hocPhiChuaDong = 0.0;
				   cons.clear();
				   { 
		             ConditionReport con = new ConditionReport(); 
		             con.setCol("hocVienHocPhiId.hocVienId.id");
		             con.setValue1(hocVien.getId());
		             con.setOperator("eq");
		             cons.add(con);	
				   } 
				   { 
		             ConditionReport con = new ConditionReport(); 
		             con.setCol("hocVienHocPhiId.hocKyId.id");
		             con.setValue1(hocKyId);
		             con.setOperator("lt");
		             cons.add(con);	
				   } 
				   { 
		             ConditionReport con = new ConditionReport(); 
		             con.setCol("daDongHP");
		             con.setValue1(false);
		             con.setOperator("eq");
		             cons.add(con);	
				   }
				   List<TblQLDTTXAHocVienHocPhiMonHoc> hocPhiMonHocChuaDongs = tblQLDTTXAHocVienHocPhiMonHocService.findtblQLDTTXAHocVienHocPhiMonHocByCondition(cons, 0, 0);
				   
				   if(hocPhiMonHocChuaDongs != null && hocPhiMonHocChuaDongs.size() > 0){
					   for(TblQLDTTXAHocVienHocPhiMonHoc hocPhiMonHocChuaDong : hocPhiMonHocChuaDongs){
						   hocPhiChuaDong += hocPhiMonHocChuaDong.getThanhTien() + tienHaTang;
					   }
				   }
				   
				   // lay thong tin tai khoan
				   Double taiKhoanHocVien = 0.0;
				   cons.clear();
				   { 
		             ConditionReport con = new ConditionReport(); 
		             con.setCol("hocVienId.id");
		             con.setValue1(hocVien.getId());
		             con.setOperator("eq");
		             cons.add(con);	
		         } 
				   List<TblQLDTTXAHocVienTaiKhoan> taiKhoans = tblQLDTTXAHocVienTaiKhoanService.findtblQLDTTXAHocVienTaiKhoanByCondition(cons, 0, 0);
				   if(taiKhoans != null && taiKhoans.size() > 0){
					   taiKhoanHocVien = taiKhoans.get(0).getSoDuTaiKhoan();
				   }
				   
				   Double no = hocPhiChuaDong - taiKhoanHocVien;
				   report += "<TD>" +myFormatter.format(hocPhiChuaDong)+ "</TD>";
				   report += "<TD>" +myFormatter.format(hocPhiChuaDong)+ "</TD>";
				   if(taiKhoanHocVien.equals(new Double(0.0))){
					   report += "<TD>0</TD>";
				   }
				   else if(taiKhoanHocVien > 0){
					   report += "<TD>" +myFormatter.format(taiKhoanHocVien)+ "</TD>";
				   } else {
					   report += "<TD>(" +myFormatter.format(taiKhoanHocVien*(-1))+ ")</TD>";
				   }
				   report += "<TD></TD>";
				   if(no > 0){
					   report += "<TD>" + myFormatter.format(no) + "</TD>";
				   } else {
					   report += "<TD>0</TD>";
				   }
			  }
			  
			  else { 
			   Double tienHaTang = hocVienHocPhi.getTienHaTang()/ (hocVienHocPhi.getSoMonHoc()-hocVienHocPhi.getSoMonHocDuocMien());
			   if (tienHaTang.isNaN())
			   {
				   tienHaTang = tblQLDTTXAQuiDinhHocPhi.getTienHaTang();
			   }
			   cons.clear();
			   if(hocVienHocPhi != null){ 
			       ConditionReport con = new ConditionReport(); 
			       con.setCol("hocVienHocPhiId.id");
			       con.setValue1(hocVienHocPhi.getId());
			       con.setOperator("eq");
			       cons.add(con);	
			   } 
		
    	      List<TblQLDTTXAHocVienHocPhiMonHoc> tblQLDTTXAHocVienHocPhiMonHocs = tblQLDTTXAHocVienHocPhiMonHocService.findtblQLDTTXAHocVienHocPhiMonHocByCondition(cons, 0, 0);
		      DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
		      
		      stt++;
		      report += "<tr class='tableitem' > "; 
		      //stt
		      report += "<TD>" +stt+ "</TD>"; 
		      //mshv
		      report += "<td>" + hocVienHocPhi.getHocVienId().getMSHV()+ "</td> ";
		      //lop
		      report += "<td>" +hocVienHocPhi.getHocVienId().getLopId().getTen()+ "</td> ";
		      //ho ten
		      report += "<td>" +hocVienHocPhi.getHocVienId().getHoTen()+ "</td> ";
		      report += "<td>" +hocVienHocPhi.getHocVienId().getTen()+ "</td> ";
		      //ngay sinh
		      try{
			      report += "<td>" 
			    	  +hocVienHocPhi.getHocVienId().getHoSoId().getNgaySinh().getDay()+"/"
			    	  +hocVienHocPhi.getHocVienId().getHoSoId().getNgaySinh().getMonth()+"/"
			    	  +hocVienHocPhi.getHocVienId().getHoSoId().getNgaySinh().getYear()+ "</td>";
			      
		      }catch(Exception e){
		    	  report += "<td></td>"; 
		            
		      }
		      
		      Double hocPhiX = 0.0;
		      Double hocPhiKM = 0.0;
		      
		      	      
		      
		      //hoc phi tung mon hoc
		      for(TblQLDTTXAMonHoc monHoc : tblQLDTTXAMonHocs){
		    	  
			      Boolean b = false;
			      Boolean mienMon = false;
			      Boolean khongMien = false;
				   Double hocPhiMonHoc = 0.0;
				   for(TblQLDTTXAHocVienHocPhiMonHoc chiTietHocPhi : tblQLDTTXAHocVienHocPhiMonHocs){
						
					   if(chiTietHocPhi.getMonHocId().getId().equals(monHoc.getId())){
					       b = true;
					       mienMon = chiTietHocPhi.isMienMon();
					       if(!mienMon)
					    	   hocPhiMonHoc = chiTietHocPhi.getThanhTien() + tienHaTang;
					       if(!chiTietHocPhi.getTienTraLai().equals(new Double(0.0)))
					    	   khongMien = true;
					       break;
					   }
				   }//end for chi tiet hoc phi
				   if(b){
					   if(mienMon)
						   report += "<TD>MM</TD>";
					   else {
						   if(khongMien){
							   report += "<TD>KM</TD>";
							   hocPhiKM += hocPhiMonHoc;							   
						   }
						   else{
							   report += "<TD>X</TD>";
							   hocPhiX += hocPhiMonHoc;
						   }
					   }
						   //report += "<TD>" +myFormatter.format(hocPhiMonHoc) + "</TD>";
					   
				   }else
					   report += "<TD></TD>";
				   
				   //Double tongHPMonHoc = tongHocPhiMonHoc.get(monHoc.getId()).doubleValue() + hocPhiMonHoc;
				   //tongHocPhiMonHoc.put(monHoc.getId(), tongHPMonHoc);
				   //tienPhaiDong += hocPhiMonHoc;
				   
			   }//end-for mon hoc

			   //tong hoc phi hoc ky
			   report += "<TD>" +myFormatter.format(hocPhiX)+ "</TD>";
			   report += "<TD>" +myFormatter.format(hocPhiKM)+ "</TD>";
			   
			   Double hocPhiChuaDong = 0.0;
			   cons.clear();
			   { 
	             ConditionReport con = new ConditionReport(); 
	             con.setCol("hocVienHocPhiId.hocVienId.id");
	             con.setValue1(hocVienHocPhi.getHocVienId().getId());
	             con.setOperator("eq");
	             cons.add(con);	
			   } 
			   { 
	             ConditionReport con = new ConditionReport(); 
	             con.setCol("hocVienHocPhiId.hocKyId.id");
	             con.setValue1(hocKyId);
	             con.setOperator("lt");
	             cons.add(con);	
			   } 
			   { 
	             ConditionReport con = new ConditionReport(); 
	             con.setCol("daDongHP");
	             con.setValue1(false);
	             con.setOperator("eq");
	             cons.add(con);	
			   }
			   List<TblQLDTTXAHocVienHocPhiMonHoc> hocPhiMonHocChuaDongs = tblQLDTTXAHocVienHocPhiMonHocService.findtblQLDTTXAHocVienHocPhiMonHocByCondition(cons, 0, 0);
			   
			   if(hocPhiMonHocChuaDongs != null && hocPhiMonHocChuaDongs.size() > 0){
				   for(TblQLDTTXAHocVienHocPhiMonHoc hocPhiMonHocChuaDong : hocPhiMonHocChuaDongs){
					   hocPhiChuaDong += hocPhiMonHocChuaDong.getThanhTien() + tienHaTang;
				   }
			   }
			   
			   Double taiKhoanHocVien = 0.0;
			   cons.clear();
			   { 
	             ConditionReport con = new ConditionReport(); 
	             con.setCol("hocVienId.id");
	             con.setValue1(hocVienHocPhi.getHocVienId().getId());
	             con.setOperator("eq");
	             cons.add(con);	
	         } 
			   List<TblQLDTTXAHocVienTaiKhoan> taiKhoans = tblQLDTTXAHocVienTaiKhoanService.findtblQLDTTXAHocVienTaiKhoanByCondition(cons, 0, 0);
			   if(taiKhoans != null && taiKhoans.size() > 0){
				   taiKhoanHocVien = taiKhoans.get(0).getSoDuTaiKhoan();
			   }
			   
			   Double no = hocPhiChuaDong - taiKhoanHocVien;
			   
			   /*if(no.equals(new Double(0.0))){
				   report += "<TD>0</TD>";
			   }
			   else if(no > 0){
				   report += "<TD>" +myFormatter.format(no)+ "</TD>";
			   } else {
				   report += "<TD>(" +myFormatter.format(no*(-1))+ ")</TD>";
			   }*/
			   report += "<TD>" +myFormatter.format(hocPhiChuaDong)+ "</TD>";
			   report += "<TD>" +myFormatter.format(hocPhiX + hocPhiKM + hocPhiChuaDong)+ "</TD>";
			   if(taiKhoanHocVien.equals(new Double(0.0))){
				   report += "<TD>0</TD>";
			   }
			   else if(taiKhoanHocVien > 0){
				   report += "<TD>" +myFormatter.format(taiKhoanHocVien)+ "</TD>";
			   } else {
				   report += "<TD>(" +myFormatter.format(taiKhoanHocVien*(-1))+ ")</TD>";
			   }
			   
			   cons.clear();
			   { 
	             ConditionReport con = new ConditionReport(); 
	             con.setCol("hocVienId.id");
	             con.setValue1(hocVienHocPhi.getHocVienId().getId());
	             con.setOperator("eq");
	             cons.add(con);	
	         } { 
	             ConditionReport con = new ConditionReport(); 
	             con.setCol("hocKyId.id");
	             con.setValue1(hocKyId);
	             con.setOperator("eq");
	             cons.add(con);	
	         } 
	         int bienLaiNum = tblQLDTTXAHocVienHocPhiBienLaiService.counttblQLDTTXAHocVienHocPhiBienLaiByCondition(cons);
	         
	         if (bienLaiNum > 0){
	        	 report += "<TD>X</TD>";
	        	 if(taiKhoanHocVien < 0){
	        		 report += "<TD>" + myFormatter.format(-taiKhoanHocVien) + "</TD>";
	        	 }else{
	        		 report += "<TD>0</TD>";
	        	 }
	         }else{
	        	 report += "<TD></TD>";
	        	 Double tienPhaiDong = hocPhiX + hocPhiKM + no;
				   if(tienPhaiDong < 0)
					   tienPhaiDong = 0.0;
				   
				   report += "<TD>" +myFormatter.format(tienPhaiDong)+ "</TD>";
	         }
	         
			   
			   
			   report +=  "</tr> ";
			   
			   //tongHPPhaiThu += tienPhaiDong;
		  }
		  }

		  report += "</tbody>";
  
		  return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Report tblQLDTTXABienNhanHoSo Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public String getReport() {
	return report;
}

public void setReport(String report) {
	this.report = report;
}
public Long getLopId() {
	return lopId;
}
public void setLopId(Long lopId) {
	this.lopId = lopId;
}
public Long getKhoaId() {
	return khoaId;
}
public void setKhoaId(Long khoaId) {
	this.khoaId = khoaId;
}
public Long getNganhId() {
	return nganhId;
}
public void setNganhId(Long nganhId) {
	this.nganhId = nganhId;
}
public Long getKhoaHocId() {
	return khoaHocId;
}
public void setKhoaHocId(Long khoaHocId) {
	this.khoaHocId = khoaHocId;
}
public Long getChiNhanhId() {
	return chiNhanhId;
}
public void setChiNhanhId(Long chiNhanhId) {
	this.chiNhanhId = chiNhanhId;
}
public Long getHocKyId() {
	return hocKyId;
}
public void setHocKyId(Long hocKyId) {
	this.hocKyId = hocKyId;
}
public List<TblQLDTTXADMKhoa> getTblQLDTTXADMKhoas() {
	return tblQLDTTXADMKhoas;
}
public void setTblQLDTTXADMKhoas(List<TblQLDTTXADMKhoa> tblQLDTTXADMKhoas) {
	this.tblQLDTTXADMKhoas = tblQLDTTXADMKhoas;
}
public List<TblQLDTTXALop> getTblQLDTTXALops() {
	return tblQLDTTXALops;
}
public void setTblQLDTTXALops(List<TblQLDTTXALop> tblQLDTTXALops) {
	this.tblQLDTTXALops = tblQLDTTXALops;
}
public List<TblQLDTTXADMNganh> getTblQLDTTXADMNganhs() {
	return tblQLDTTXADMNganhs;
}
public void setTblQLDTTXADMNganhs(List<TblQLDTTXADMNganh> tblQLDTTXADMNganhs) {
	this.tblQLDTTXADMNganhs = tblQLDTTXADMNganhs;
}
public List<TblQLDTTXADMKhoaHoc> getTblQLDTTXADMKhoaHocs() {
	return tblQLDTTXADMKhoaHocs;
}
public void setTblQLDTTXADMKhoaHocs(
		List<TblQLDTTXADMKhoaHoc> tblQLDTTXADMKhoaHocs) {
	this.tblQLDTTXADMKhoaHocs = tblQLDTTXADMKhoaHocs;
}
public List<TblQLDTTXADMChiNhanh> getTblQLDTTXADMChiNhanhs() {
	return tblQLDTTXADMChiNhanhs;
}
public void setTblQLDTTXADMChiNhanhs(
		List<TblQLDTTXADMChiNhanh> tblQLDTTXADMChiNhanhs) {
	this.tblQLDTTXADMChiNhanhs = tblQLDTTXADMChiNhanhs;
}
public List<TblQLDTTXAHocKyTrongNam> getTblQLDTTXAHocKyTrongNams() {
	return tblQLDTTXAHocKyTrongNams;
}
public void setTblQLDTTXAHocKyTrongNams(
		List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams) {
	this.tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNams;
}
public List<TblQLDTTXALopMonHoc> getTblQLDTTXALopMonHocs() {
	return tblQLDTTXALopMonHocs;
}
public void setTblQLDTTXALopMonHocs(
		List<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs) {
	this.tblQLDTTXALopMonHocs = tblQLDTTXALopMonHocs;
}
public List<TblQLDTTXAMonHoc> getTblQLDTTXAMonHocs() {
	return tblQLDTTXAMonHocs;
}
public void setTblQLDTTXAMonHocs(List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs) {
	this.tblQLDTTXAMonHocs = tblQLDTTXAMonHocs;
}
public void setMsg(Message msg) {
	this.msg = msg;
} 
 } 
 
